package com.jinmdz.app.api.api.service;

import com.jinmdz.app.api.api.model.coldstorage.ColdStorageInData;
import com.jinmdz.app.api.api.model.common.QrCodeData;
import com.jinmdz.app.api.model.system.UserItem;
import com.jinmdz.app.dao.dao.FuneralBusinessDao;
import com.jinmdz.app.dao.dao.MournCeremonyDao;
import com.jinmdz.app.dao.dao.ServiceItemDao;
import com.jinmdz.app.dao.model.coldStorageEquipment.ColdStorageEquipmentView;
import com.jinmdz.app.dao.model.coldstorage.ColdStorageView;
import com.jinmdz.app.api.api.model.common.BusinessCodeData;
import com.jinmdz.app.api.base.BaseService;
import com.jinmdz.app.api.constant.MessageConst;
import com.jinmdz.app.dao.dao.ColdStorageDao;
import com.jinmdz.app.dao.model.funeral.FuneralBusinessViewItem;
import com.jinmdz.app.dao.model.mournceremony.MournCeremonyView;
import com.jinmdz.app.dao.model.service.ServiceItemView;
import com.jinmdz.fmis.common.util.DataUtil;
import com.jinmdz.fmis.core.base.BaseBean;
import com.jinmdz.fmis.core.base.BaseResult;
import com.jinmdz.fmis.core.exception.ActionException;
import com.jinmdz.fmis.mapper.entity.ChargeItemEntity;
import com.jinmdz.fmis.mapper.entity.ColdStorageEntity;
import com.jinmdz.fmis.mapper.entity.ColdStorageEquipmentEntity;
import com.jinmdz.fmis.mapper.mapper.ChargeItemMapper;
import com.jinmdz.fmis.mapper.mapper.ColdStorageEquipmentMapper;
import com.jinmdz.fmis.mapper.mapper.ColdStorageMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Objects;

/**
 * ColdStorageService类
 *
 * @author Wangshengtao
 * @date 2020-08-24 09:18
 */
@Service("ColdStoragePdaService")
public class ColdStorageService extends BaseService {

    @Resource
    private FuneralBusinessDao funeralBusinessDao;

    @Resource
    private ColdStorageDao coldStorageDao;

    @Resource
    private MournCeremonyDao mournCeremonyDao;

    @Resource
    private ColdStorageEquipmentMapper coldStorageEquipmentMapper;

    @Resource
    private ColdStorageMapper coldStorageMapper;

    @Resource
    private ServiceItemDao serviceItemDao;

    @Resource
    private ChargeItemMapper chargeItemMapper;


    /**
     * 根据 逝者编码（业务编码）查询冷藏信息
     *
     * @param data 冷藏实体
     * @return
     * @author Wangshengtao
     * @date 2020-08-24 09:18
     */
    public BaseResult<ColdStorageView> loadColdStorageWithBusinessCode(BusinessCodeData data) {

        // 通过业务编码获取殡葬信息
        FuneralBusinessViewItem funeralItem = funeralBusinessDao.getFuneralBusinessViewByBusinessCode(data.getBusinessCode());
        if (DataUtil.isNull(funeralItem) || DataUtil.isNull(funeralItem.getId(), funeralItem.getVersion())) {
            return failure(MessageConst.BUSINESS_CODE_ERROR);
        }

        // 根据 业务编码 查询 逝者冷藏信息
        ColdStorageView coldStorageView = coldStorageDao.getColdStorageViewByBusinessCode(data.getBusinessCode());
        if (DataUtil.isNull(coldStorageView)) {
            return failure("无此" + coldStorageView.getBusinessCode() + "业务编号的逝者冷藏信息，请先预约冷藏！");
        }

        return successData(coldStorageView);
    }

    /**
     * 根据 冷柜设备编码 查询冷柜信息
     *
     * @param
     * @return
     * @author Wangshengtao
     * @date 2020-08-24 10:18
     */
    public BaseResult<HashMap<String,Object>> loadColdStorageEquipmentWithEquipmentCode(HashMap<String,Object> qrCodeMapData) {
        HashMap<String,Object> coldStorageEquipment = coldStorageDao.getColdStorageEquipmentByEquipmentCode(qrCodeMapData);
        if (Objects.isNull(coldStorageEquipment)) {
            return failure("请检查设备编号，无此编码对应的冷柜信息！");
        }
        return successData(coldStorageEquipment);
    }

    /**
     * 入藏
     *
     * @param data 业务编码
     * @return
     * @author Wangshengtao
     * @date 2020-08-24 16:54
     */
    @Transactional(rollbackFor = Exception.class)
    public BaseResult saveDeadToEquipment(UserItem userItem, ColdStorageInData data) throws ActionException {
        /**
         * 入藏：入藏前检查 是否 有进行中未完成的任务，：已入厅 尚未出厅任务，如果发现有进行中，未完成的任务，则提示
         * 工作人员 先将进行中任务完成后，再进行入藏操作。
         */
        MournCeremonyView mournCeremonyView = mournCeremonyDao.getMournCeremonyViewByBusinessCode(data.getBusinessCode());
        if (DataUtil.isNull(mournCeremonyView)) {
            return failure("没有此业务编码对应的礼厅信息，请检查!");
        }
        if (DataUtil.equals(mournCeremonyView.getProcessStateCode(), 2)) {
            return failure(mournCeremonyView.getBusinessCode() + "还未出厅，请先将进行中的任务完成后，再进行入藏操作！");
        }


        // 先根据 业务编码 查询出 冷柜信息
        // 然后查询 逝者的预约冷柜类型 和 扫描的冷柜类型是否 一致

        return null;
    }
}
